﻿// Principia
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Linq;

namespace Principia.Core.Parsing.ContextFree.LR
{
    public class Item : Tuple<int, Production, ITerminal>
    {
        public Item(int Position, Production Production, ITerminal Lookahead)
            : base(Position, Production, Lookahead)
        {
        }

        public int Position
        {
            get { return Item1; }
        }

        public Production Production
        {
            get { return Item2; }
        }

        public ITerminal Lookahead
        {
            get { return Item3; }
        }

        public INonterminal NonterminalAtPosition
        {
            get
            {
                if (Position < Production.Symbols.Length)
                    return Production.Symbols[Position] as INonterminal;
                return null;
            }
        }

        public ISymbol[] SymbolsAfterPosition
        {
            get
            {
                var Len = Production.Symbols.Length - Position - 1;

                ISymbol[] BetaA;

                if (Lookahead != null)
                {
                    BetaA = new ISymbol[Len + 1];
                    BetaA[Len] = Lookahead;
                }
                else
                {
                    BetaA = new ISymbol[Len];
                }

                for (var i = 0; i < Len; i++)
                    BetaA[i] = Production.Symbols[i + Position + 1];

                return BetaA;
            }
        }

        public bool ShouldReduce(ITerminal OnLookahead)
        {
            return Lookahead == OnLookahead
                   && Position == Production.Symbols.Length;
        }

        public bool ShouldShift(ITerminal Terminal)
        {
            return Position < Production.Symbols.Length
                   && Terminal == Production.Symbols[Position];
        }

        public override string ToString()
        {
            return string.Format("[{0} -> {1}.{2}, {3}]",
                                 Production.Head,
                                 Production.Symbols.Take(Position).Join(" "),
                                 Production.Symbols.Skip(Position).Join(" "),
                                 Lookahead == null ? "$" : Lookahead.ToString());
        }

        [Obsolete]
        public Item Duplicate()
        {
            return new Item(Position, new Production
                                      {
                                          Head = Production.Head,
                                          SemanticFunc = Production.SemanticFunc,
                                          Symbols = Production.Symbols
                                      }, Lookahead);
        }
    }
}